package com.hhdys.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hhdys.dao.RoleMapper;
import com.hhdys.model.Role;
import com.hhdys.model.RoleExample;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper dao;

	/**
	 * @return the dao
	 */
	public RoleMapper getDao() {
		return dao;
	}

	/**
	 * @param dao
	 *            the dao to set
	 */
	public void setDao(RoleMapper dao) {
		this.dao = dao;
	}

	@Override
	public List<Role> selRoles() {
		return dao.selectByExample(null);
	}

	@Override
	public boolean addRole(String name, String addUser) {
		Role role = new Role();
		role.setLastModifier(addUser);
		role.setLastModifyTime(new Date());
		role.setName(name);
		return dao.insert(role) > 0;
	}

	@Override
	public boolean delRoles(String ids) {
		String[] idStrs = ids.split(",");
		List<Integer> list = new ArrayList<Integer>();
		int count = idStrs.length;
		for (int i = 0; i < count; i++) {
			list.add(NumberUtils.toInt(idStrs[i]));
		}
		RoleExample example = new RoleExample();
		RoleExample.Criteria criteria = example.createCriteria();
		criteria.andIdIn(list);
		return dao.deleteByExample(example) > 0;
	}

	@Override
	public boolean editRole(int id, String name, String modifyUser) {
		Role role = new Role();
		role.setId(id);
		role.setLastModifier(modifyUser);
		role.setLastModifyTime(new Date());
		role.setName(name);
		return dao.updateByPrimaryKeySelective(role) > 0;
	}
}
